package com.lovestudying.service.impl;

import java.util.HashMap;
import java.util.Map;

import com.lovestudying.dao.CommentDao;
import com.lovestudying.dao.ReplyDao;
import com.lovestudying.dao.Impl.CommentDaoImpl;
import com.lovestudying.dao.Impl.ReplyDaoImpl;
import com.lovestudying.entity.Comment;
import com.lovestudying.service.CommentService;
import com.lovestudying.util.PageInfo;

/**
 * 一级评论(Comment)的业务逻辑
 * 
 * @author OldDeer
 *
 */
public class CommentServiceImpl implements CommentService {
	CommentDao CommentDao = new CommentDaoImpl();
	ReplyDao ReplyDao = new ReplyDaoImpl();
	
	

	@Override
	public PageInfo<Comment> GetCommentsByUUID(String uuid, String orderParam, String desc, Integer pageSize,
			Integer pageNum) {
		// TODO 自动生成的方法存根
		Map<String, Object> whereParams = new HashMap<>();
		Map<String, Object> orderParams = new HashMap<>();
		whereParams.put("`comment`.comment_group", uuid);
		whereParams.put("`comment`.comment_state", 1);
		orderParams.put(orderParam, desc);
		PageInfo<Comment> Comment = CommentDao.findCommentUniAccount(whereParams, orderParams, pageSize,
				pageNum);
		
		return Comment;
	}

	@Override
	public Boolean PostComment(String commentUuid, String createTime, String accountId, String commentCotent,
			String commentGroup) {
		Map<String, Object> Value_Param = new HashMap<>();
		Value_Param.put("comment_id", commentUuid);
		Value_Param.put("create_time", createTime);
		Value_Param.put("account_id", accountId);
		Value_Param.put("comment_content", commentCotent);
		Value_Param.put("comment_group", commentGroup);
		if (CommentDao.InsertComment(Value_Param) != 1) {
			return false;
		} else {
			return true;
		}

	}

	@Override
	public String deleteComOrRepById(String deleteId) {
		Map<String, Object> setForComment = new HashMap<>();
		Map<String, Object> setForReply = new HashMap<>();
		Map<String, Object> whereForComment = new HashMap<>();
		Map<String, Object> whereForReply = new HashMap<>();
		setForComment.put("comment_state", 0);
		whereForComment.put("comment_id", deleteId);
		setForReply.put("reply_state", 0);
		whereForReply.put("reply_id", deleteId);
		if (CommentDao.UpdateComment(setForComment, whereForComment) == 1) {
			Map<String, Object> newSet = new HashMap<>();
			newSet.put("reply_state", 0);
			Map<String, Object> newWhere = new HashMap<>();
			newWhere.put("parent_comment", deleteId);
			ReplyDao.UpdateReply(newSet, newWhere);
			return "deleteComment";
		} else if (ReplyDao.UpdateReply(setForReply, whereForReply) == 1) {
			return "deleteReply";
		} else {
			return "deleteFail";
		}
	}
}
